<?php

class ProjectController extends Zend_Controller_Action {

    private $_isUserAdmin = null;
    private $_userId = null;

    public function init() {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $validations = new Validations_User();
            $this->_userId = $validations->getUserId();
            $layout = $validations->getUserLayout();
            if ($layout == "admin") {
                $this->_isUserAdmin = true;
            }
            $this->_helper->layout->setLayout($layout);
        } else {
            $this->_redirect("/home");
        }
    }

    public function indexAction() {
        $projectDb = new Application_Model_Project();
        $userId = ($this->_isUserAdmin) ? 0 : $this->_userId;
        $fetchAll = $projectDb->fetchAll($userId);
        $this->view->isAdmin = $this->_isUserAdmin;
        $this->view->fetchAll = $fetchAll;
    }

    public function infoAction() {
        $projectId = $this->getRequest()->getParam("id");
        $projectDb = new Application_Model_Project();
        $project = $projectDb->getById($projectId);
        $this->view->project = $project;
    }

    public function addAction() {
        $form = new Application_Form_Projectadd();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $data = array(
                    'details' => json_encode(array('name' => $values['name'], 'projectLink' => $values['projectLink'], 'description' => $values['description'])),
                    'user_id' => $values['userId'],
                    'status' => $values['status'],
                );
                $projectDb = new Application_Model_Project();
                $projectDb->createProjet($data);
                $this->view->isSuccess = true;
            }
        }
    }

    public function editAction() {
        $projectId = $this->getRequest()->getParam("id");
        $projectDb = new Application_Model_Project();
        $project = $projectDb->getById($projectId);
        $form = new Application_Form_Projectadd();
        $form->name->setValue($project['name']);
        $form->projectLink->setValue($project['projectLink']);
        $form->description->setValue($project['description']);
        $form->userId->setValue($project['userId']);
        $form->status->setValue($project['status_id']);
        $form->id->setValue($projectId);
        $form->submit->setLabel("Update Project");
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $data = array(
                    'details' => json_encode(array('name' => $values['name'], 'projectLink' => $values['projectLink'], 'description' => $values['description'])),
                    'user_id' => $values['userId'],
                    'status' => $values['status'],
                );
                $projectDb = new Application_Model_Project();
                $projectDb->updateData($data, $values['id']);
                $this->view->isSuccess = true;
            }
        }
    }

    public function finalizeAction() {
        $projectId = $this->getRequest()->getParam("id");
        $projectDb = new Application_Model_Project();
        $projectDb->updateData(array('finalized' => true), $projectId);
        $this->_redirect("/project/info/$projectId");
    }

}

